/**
 * Created by zhongwei on 2024/04/07.
 */
/**
 * 获取当前浏览器语言
 */
import CryptoJS from "crypto-js";

export function getLocale() {
  const language = navigator.language;
  if(localStorage.getItem('language')){
    return localStorage.getItem('language');
  }else{
    if (language.includes("zh-CN") || language.includes("zh")) {
      localStorage.setItem('language',"zh-CN");
      return "zh-CN";
    }else if(language.includes("en")){
      localStorage.setItem('language',"en");
      return 'en'
    }else if(language.includes("zh-TW")){
      localStorage.setItem('language',"zh-TW");
      return 'zh-TW'
    }else if(language.includes("ja")){
      localStorage.setItem('language',"ja");
      return 'ja'
    }else if(language.includes("ko")){
      localStorage.setItem('language',"ko");
      return 'ko'
    }else if(language.includes("de")){
      localStorage.setItem('language',"de");
      return 'de'
    }else if(language.includes("ar")){
      localStorage.setItem('language',"ar");
      return 'ar'
    }else if(language.includes("es")){
      localStorage.setItem('language',"es");
      return 'es'
    }else if(language.includes("fr")){
      localStorage.setItem('language',"fr");
      return 'fr'
    }else if(language.includes("it")){
      localStorage.setItem('language',"it");
      return 'it'
    }else if(language.includes("nl")){
      localStorage.setItem('language',"nl");
      return 'nl'
    }else if(language.includes("pl")){
      localStorage.setItem('language',"pl");
      return 'pl'
    }else if(language.includes("pt-PT")){
      localStorage.setItem('language',"pt_PT");
      return 'pt_PT'
    }else if(language.includes("ro")){
      localStorage.setItem('language',"ro");
      return 'ro'
    }else if(language.includes("ru")){
      localStorage.setItem('language',"ru");
      return 'ru'
    }else if(language.includes("sk")){
      localStorage.setItem('language',"sk");
      return 'sk'
    }else{
      localStorage.setItem('language',"en");
      return "en";
    }
    
  }
  
}


/**
 * 数字
 * @param num 输入的数字
 * @param bol true 保留3位小数
 */
export function formatDecimal (num, decimal) {
  num = num.toString()
  let index = num.indexOf('.')
  if (index !== -1) {
      num = num.substring(0, decimal + index + 1)
  } else {
      num = num.substring(0)
  }
  return parseFloat(num).toFixed(decimal)
}
/**
* excel下载
* @param arraybuffer excel的buffer文件
* @param name 文件名称
*/
export function downloadFile (arraybuffer, name) {
  let url = window.URL.createObjectURL(new Blob([arraybuffer]))
  let link = document.createElement('a')
  link.style.display = 'none'
  link.href = url
  link.setAttribute('download', name)
  document.body.appendChild(link)
  link.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, view: window }));
}

/**
* buildurl文件加载方法
* @param path 文件路径
* @param queryMap 文件名称
*/

export function buildurl(path, queryMap) {
  let query = '';
  if (queryMap) {
    const queries = [];
    for (const k of Object.keys(queryMap)) {
      const v = queryMap[k];
      if (v === null || v === undefined) {
        continue;
      }
      if (v instanceof Array) {
        for (const vv of v) {
          queries.push(k + '=' + encodeURIComponent(vv));
        }
      } else {
        queries.push(k + '=' + encodeURIComponent(queryMap[k]));
      }
    }
    query = '?' + queries.join('&');
  }
  return import.meta.env.VITE_APIHOST + path + query;
}

/**
* getImageUrl图片加载方法
* @param path 图片文件夹
* @param name 文件路径
* @param type 文件类型
*/
export const getImageUrl = (path,name,type='png') => {
  return new URL(
    // 本地资源路径
    `/assets/img/${path}/${name}.${type}`,
    import.meta.url
  ).href;
};

export const detectZoom = () => {
  let ratio = 0
  const screen = window.screen
  const ua = navigator.userAgent.toLowerCase()
  if (window.devicePixelRatio !== undefined) {
    ratio = window.devicePixelRatio
  } else if (~ua.indexOf('msie')) {
    if (screen.deviceXDPI && screen.logicalXDPI) {
      ratio = screen.deviceXDPI / screen.logicalXDPI
    }
  } else if (
    window.outerWidth !== undefined &&
    window.innerWidth !== undefined
  ) {
    ratio = window.outerWidth / window.innerWidth
  }
  if (ratio) {
    ratio = Math.round(ratio * 100)
  }
  return ratio
}

// 判断所处环境
export function detectEnvironment() {
  if(typeof process !== "undefined" && process.versions && process.versions.node){
    return true;
  }else{
    return false;
  }
}

// 解密
export function encydecrypt(data) {
  const key = CryptoJS.enc.Utf8.parse("4525879010997438");
  const iv = CryptoJS.enc.Utf8.parse("7825679025843169");
  // 解密
  const decrypted = CryptoJS.AES.decrypt(data, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7,
  }).toString(CryptoJS.enc.Utf8);
  return decrypted;
}